/*
 * TextureManager.h
 *
 *  Created on: 2009/01/13
 *      Author: Yongshik Cho
 */

#ifndef TEXTUREMANAGER_H_
#define TEXTUREMANAGER_H_

#include <vector>
#include <setjmp.h>

#ifndef PREREQUISITES_H_
#	include "core/Prerequisites.h"
#endif

#ifndef ISINGLETON_H_
#	include "core/ISingleton.h"
#endif

#ifndef TEXTURE_H_
#	include "Texture.h"
#endif

#include "png.h"
#include "gif_lib.h"

#include "sqplus/sqplus.h"
using namespace SqPlus;


class TextureManager : public ISingleton<TextureManager>
{
public:
	static const String _EXT_PNG;
	static const String _EXT_JPG;
	static const String _EXT_JPEG;
	static const String _EXT_GIF;
	static const String _EXT_BMP;

public:
	TextureManager();
	virtual ~TextureManager();

public:
	static TextureManager& GetInstance()
	{
		assert(m_pInstance);
		return *m_pInstance;
	}
	static TextureManager* GetInstancePtr()
	{
		return m_pInstance;
	}

public:
	inline const uint32 getTextureCount() { return (uint32)mTextures.size(); }

	virtual Texture* load(const String& p);

private:
	Texture* isExists(const String& p);
	Texture* loadBitmap(const String& p);
	Texture* loadPng(const String& p);
	Texture* loadJpg(const String& p);
	Texture* loadGif(const String& p);

	static void pngReadDataFn(png_structp png_ptr, png_bytep data, png_size_t length);
	static int32 gifReadDataFn(GifFileType* gifinfo, GifByteType* data, int32 length);

	bool isScaleNeeded(const uint32 width, const uint32 height, uint32& scalew, uint32& scaleh);
	bool is2Multiplied(const uint32 value);
	uint32 getFloor2Multiflied(const uint32 value);

private:
	vector<Texture*> mTextures;
};

//must be declared here
DECLARE_INSTANCE_TYPE(TextureManager)

#endif /* TEXTUREMANAGER_H_ */
